package RedisDay1;

import redis.clients.jedis.Jedis;

import java.util.Random;
import java.util.Scanner;

public class SMS {
    public static void main(String[] args) {
        //验证登录
        //随机生成一串四位数字
        Random ra = new Random();
        Integer VerifyCode = 1000 + ra.nextInt(8999);
        //显示验证码
        System.out.println(VerifyCode);
        //将数字传给数据库
        //连接Redis
        Jedis master = new Jedis("192.168.111.166", 7000);
        if(master.ping().equals("PONG")){
//            System.out.println("连接成功！");
            master.setex("VerifyCode",10,VerifyCode.toString());
        }else{
            System.out.println("网络异常！");
        }
        //登录验证--循环
        System.out.println("请输入验证码：");
        Scanner sc = new Scanner(System.in);
        while(true){
            if(!master.exists("VerifyCode")){
                System.out.println("验证码过期！");
                break;
            }
            else
            {
                String yzm = sc.next();
                //取出验证码，进行验证
                if(yzm.equals(master.get("VerifyCode"))){
                    System.out.println("登陆成功");
                    break;
                }
                else{
                    System.out.println("请重新输入！");
                    System.out.println("剩余时间："+master.ttl("VerifyCode")+"秒");
                }
            }
        }
    }
}
